package cn.com.ntr.jhclient.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cn.com.ntr.jhclient.model.Trade;

public class DBTrade implements DBInterface<Trade> {

	private DatabaseHelper helper;

	public DBTrade(Context context) {
		helper = new DatabaseHelper(context);
	}

	@Override
	public long insert(Trade trade) {
		// TODO Auto-generated method stub
		ContentValues values = new ContentValues();
		values.put(Trade.TID, trade.gettId());
		values.put(Trade.T_NAME, trade.gettName());
		values.put(Trade.T_ENNAME, trade.gettEName());
		values.put(Trade.T_ORDER, trade.getOrderId());
		return helper.insertData(CreateDbConstants.TABLE_TRADE, values);
	}

	@Override
	public List<Trade> queryAll() {
		// TODO Auto-generated method stub
		List<Trade> list = new ArrayList<Trade>();
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from "
				+ CreateDbConstants.TABLE_TRADE, null);
		while (cursor.moveToNext()) {
			Trade obj = new Trade();
			obj.settName(cursor.getString(cursor.getColumnIndex(Trade.T_NAME)));
			obj.settId(cursor.getString(cursor.getColumnIndex(Trade.TID)));
			list.add(obj);
		}
		return list;
	}

}
